from flask import jsonify
from flask_restful import Resource

from database import Database
from users.book.model.Category import Category
from users.book.model.Stack import Stack


# Mock user database
class GetLibrary(Resource):

    def get(self):

        db = Database()
        categories_json = []

        query = "SELECT * FROM category"

        categories = db.execute_query(query)


        for category in categories:
            query = "SELECT * FROM stack WHERE categoryId = %s"
            stacks = db.execute_query(query, (category[0],))
            stack_objects = []

            for stack in stacks:
                stack_obj = Stack(stack[0], stack[1], stack[2], stack[3], stack[4], stack[5], stack[6], stack[7])
                stack_objects.append(stack_obj.to_dict())

            category_obj = Category(category[0], category[1], category[2], stack_objects)
            category_json = category_obj.to_dict()
            categories_json.append(category_json)

        return {"categories": categories_json}, 200
